/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */


import java.io.*;
import java.net.*;

import java.util.Date;
import javax.servlet.*;
import javax.servlet.http.*;

/**
 * To be added to the main page
 * When the add new message button is clicked, refresh the page using processRequest
 * If it is first time, ..., if not, add the message in the box to the top of the stack
 * using time stamp
 * 
 * @author Kyu
 */
public class WallServlet extends HttpServlet {
   
    //boolean isPrivate = false;
    String[] names = {"John", "Kacy", "Mina"};
    String[] messages = {
        "HitCounterFilter wraps the response in a CharResponseWrapper. The wrapped response is passed to the next object in the filter chain, which is BookStoreServlet. BookStoreServlet writes its response into the stream created by CharResponseWrapper. When chain.doFilter returns, HitCounterFilter retrieves the servlet's response from PrintWriter and writes it to a buffer. The filter inserts the value of the counter into the buffer, resets the content length header of the response, and finally writes the contents of the buffer to the response stream. ", 
        "There are many ways for a filter to modify a request or response. For example, a filter could add an attribute to the request or insert data in the response. In the Duke's Bookstore example, HitCounterFilter inserts the value of the counter into the response. ", 
        "A filter that modifies a response must usually capture the response before it is returned to the client. The way to do this is to pass a stand-in stream to the servlet that generates the response. "};
    String[] dates = {"5:30 pm Nov 23, 2007", "1:00 am Nov 25, 2007", "3:00 pm Nov 28, 2007"};
    // String newMessage = "this is new message added";
    String newMsgOwner = "Jason";
    String currentDateTime = "5:30pm Nov 28, 2007";
    //Date now;
    
    
    /** 
    * Processes requests for both HTTP <code>GET</code> and <code>POST</code> methods.
    * @param request servlet request
    * @param response servlet response
    */
    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
        response.setContentType("text/html;charset=UTF-8");
        PrintWriter out = response.getWriter();
        try {
           
            out.println("<html>");
            out.println("<head>");
            out.println("<title>Servlet WallServlet</title>");  
            out.println("<link rel=\"stylesheet\" href=\"inbox.css\" type=\"text/css\">");
    
            generateInlineJS(out);
            CSSGenerator.generateNewsGroupMainPageInlineCSS(out);
            out.println("</head>");
            
            out.println("<body>");
            //out.println("<h1>Servlet WallServlet at " + request.getContextPath () + "</h1>");
            
            out.println(
                    "<form name=\"wall\" action = \"WallServlet\" method=\"post\">" +
                    
                        "<center>" +
                        "<Table width=\"40%\" border>" +
                        /* first row: show Wall title*/
                        "   <tr>" + 
                        "   <th> Wall </th>" + 
                        "   </tr>" +

                        /* second row: new message */
                        "   <tr>"+    
                        
                        "       <table width=\"40%\" border bgcolor = \"AliceBlue\">" + 
                        "           <tr>" +
                        "           </tr>" +                        
                        "           <tr>" +
                        "           <br><b>New Message:  </b></br>" +
                        "           </tr>" +
                        "           <tr>" +                       
                        "           </tr><td><br>" + newMsgOwner + "</br></td>" +                     
                        "           <td align = \"right\"><br><input type=\"checkbox\" name=\"isPrivate\" value=\"isPrivate\"> Private<br>" + 
                                    // isPrivate checkbox to make the message only readable to the owner of the page and the writer
                     
                        "          <tr>" + 
                        "           <td colspan = \"2\"> <center> <TEXTAREA wrap=\"virtual\" name=\"msgBody\" cols = 50 rows=4 ></textarea></td>" +                                               
                        "          </tr>" +                         
                        "          <tr>" +                        
                        "           <td></td>" +    
                        "           <td align = \"right\">" +
                        "           <input type=\"submit\" name=\"Post\" value=\"Post\">" + // Submit button to post the message   
                        //  "  <input type=\"submit\" value=\"New\" name=\"newGroup\">" +
                        "           </td>" +                        
                        "          </tr>"+
                        "       </table>" +                         
                        "   </tr>" ); 
            
            for(int i = 0; i < dates.length; i++)
            {                                   
                        out.println(
                        /* Third row and on: previous messages */
                        "   <tr>" + 
                        "       <table width=\"40%\" border>" + 
                        "           <tr>" + 
                        "           <td><br>Written by: " + names[i] +
                        "           </br></td>" + 
                        "           <td align = \"right\"><br>" + dates[i] + 
                        "           </br></td>" +
                        "           </tr>" + 
                        "           <tr>" + 
                        "           <th colspan = \"2\"> <TEXTAREA wrap=\"virtual\" name=\"message\" cols = 50 rows=4>" + messages[i] + "</textarea></th>" +                                                              
                        "           </tr>" + 
                        "           <tr></tr>" +
                        /*
                        "           <tr>"+ 
                        "           <td><br><input type=\"checkbox\" name=\"Private\" value=\"Private\"> Private<br>" + 
                                    // TODO: if the check box is clicked, the message becomes private 
                        
                        "           </td>" + 
                        "           <td>" + 
                        "           </td>" + 
                        "           <td><br><input type=\"button\" name=\"Delete\" value=\"Delete\"><br>" + 
                                    // TODO: If the owner is viewing the page, owner is able to delete the message
                        
                        "           </td>" + 
                        "           </tr>"+
                        */
                        "       </table>" + 
                        "   </tr>"
                        );
            }
            out.println(
                    "</Table>" +
                    "</center>" +
                    "</form>");
            
            
            out.println("</body>");
            out.println("</html>");
          
        } finally { 
            out.close();
        }
    } 
    
 private void generateInlineJS(PrintWriter out)
    {
        out.println(
                 "<script language=\"javascript\"> " +
         "function loadwindow(target)" +
         "{" +
         "       window.open(target,\"\",\"width=800,height=600,status=1,menubar=0,resizable=1\");" +      
         "}" +
         "</script>"
       );
    }
 
    // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
    /** 
    * Handles the HTTP <code>GET</code> method.
    * @param request servlet request
    * @param response servlet response
    */
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
        processRequest(request, response);
    } 

    /** 
    * Handles the HTTP <code>POST</code> method.
    * @param request servlet request
    * @param response servlet response
    */
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
        PrintWriter out = response.getWriter();
        out.println(newMsgOwner);
        out.println(currentDateTime);
        out.println(request.getParameter("isPrivate"));
        out.printf(request.getParameter("msgBody"));
    }

    /** 
    * Returns a short description of the servlet.
    */
    public String getServletInfo() {
        return "Short description";
    }
    // </editor-fold>
}
